package com.example.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.dao.ImagesDao;
import com.example.dto.DishesDTO;
import com.example.dto.ImagesDTO;
import com.example.entity.*;
import com.example.model.page.PageData;
import com.example.mybatis.service.impl.CrudServiceImpl;
import com.example.dao.SetmealsDao;
import com.example.dto.SetmealsDTO;
import com.example.service.CategoriesService;
import com.example.service.ImagesService;
import com.example.service.SetmealsService;
import com.example.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 套餐表
 *
 * @author yyyyjinying 1020066395@qq.com
 * @since 1.0-SNAPSHOT 2025-04-29
 */
@Service
public class SetmealsServiceImpl extends CrudServiceImpl<SetmealsDao, SetmealsEntity, SetmealsDTO> implements SetmealsService {

    @Autowired
    private CategoriesService categoriesService;

    @Autowired
    private ImagesService imagesService;

    @Autowired
    private ImagesDao imagesDao;

    @Override
    public QueryWrapper<SetmealsEntity> getWrapper(Map<String, Object> params) {
        QueryWrapper<SetmealsEntity> wrapper = new QueryWrapper<>();

        String name = (String) params.get("name");
        wrapper.eq(StringUtils.isNotBlank(name), "t1.name", name);

        return wrapper;
    }

    @Override
    public void save(SetmealsDTO dto) {
        SetmealsEntity entity = ConvertUtils.sourceToTarget(dto, SetmealsEntity.class);
        entity.setId(snowflake.nextId()); // 主键id可能有错误
        List<ImagesDTO> imageLists = dto.getImageLists();
        List<ImagesDTO> thumbnailIimageLists = dto.getThumbnailLists();
        Map<String, String> imageMap = imagesService.saveUpdateImages(entity.getId(), imageLists, thumbnailIimageLists);
        entity.setImageUrl(imageMap.get("imageUrl"));
        entity.setThumbnailUrl(imageMap.get("thumbnailUrl"));
        baseDao.insert(entity);

        List<SetmealDishesEntity> list = dto.getDishIds().stream().map(dishId -> {
            SetmealDishesEntity setmealDishesEntity = new SetmealDishesEntity();
            setmealDishesEntity.setDishId(dishId);
            setmealDishesEntity.setSetmealId(entity.getId());
            return setmealDishesEntity;
        }).toList();
        baseDao.batchInsertSetmealsDishRelation(list);
    }



    @Override
    public void update(SetmealsDTO dto) {
        List<ImagesDTO> imageLists = dto.getImageLists();
        List<ImagesDTO> thumbnailIimageLists = dto.getThumbnailLists();
        Map<String, String> imageMap = imagesService.saveUpdateImages(dto.getId(), imageLists, thumbnailIimageLists);
        dto.setImageUrl(imageMap.get("imageUrl"));
        dto.setThumbnailUrl(imageMap.get("thumbnailUrl"));
        super.update(dto);

        baseDao.deleteSetmealsDishRelation(List.of(dto.getId()));
        List<SetmealDishesEntity> list = dto.getDishIds().stream().map(dishId -> {
            SetmealDishesEntity setmealDishesEntity = new SetmealDishesEntity();
            setmealDishesEntity.setDishId(dishId);
            setmealDishesEntity.setSetmealId(dto.getId());
            return setmealDishesEntity;
        }).toList();
        baseDao.batchInsertSetmealsDishRelation(list);
    }

    @Override
    public void delete(Long[] ids) {
        super.delete(ids);
        imagesService.deleteAllByBelongId(List.of(ids));
        baseDao.deleteSetmealsDishRelation(List.of(ids));
    }


    @Override
    public SetmealsDTO get(Long id) {
        SetmealsDTO data = super.get(id);
        CategoriesEntity categoriesEntity = categoriesService.selectById(data.getCategoryId());
        data.setCategoryName(categoriesEntity.getName());
        List<SetmealDishesEntity> setmealDishesEntities = baseDao.selectSetmealDishes(List.of(id));
        List<Long> list = setmealDishesEntities.stream().map(SetmealDishesEntity::getDishId).toList();
        data.setDishIds(list);

        LambdaQueryWrapper<ImagesEntity> queryImages = new LambdaQueryWrapper<ImagesEntity>().in(ImagesEntity::getImageBelong, ListUtil.of(1, 6)).eq(ImagesEntity::getBelongId, data.getId()).eq(ImagesEntity::getIsDelete, 0);
        List<ImagesEntity> imagesEntities = imagesDao.selectList(queryImages);
        List<ImagesDTO> imageLists = ConvertUtils.sourceToTarget(imagesEntities, ImagesDTO.class);
        List<ImagesDTO> collect1 = imageLists.stream().filter(item -> item.getImageBelong().equals(1)).collect(Collectors.toList());
        List<ImagesDTO> collect2 = imageLists.stream().filter(item -> item.getImageBelong().equals(6)).collect(Collectors.toList());
        // 设置大图
        data.setImageLists(collect1);
        // 设置缩略图
        data.setThumbnailLists(collect2);
        return data;
    }


    @Override
    public PageData<SetmealsDTO> page(Map<String, Object> params) {
        IPage<SetmealsEntity> getpage = getPage(params, "t1.update_date", false);
        IPage<SetmealsEntity> page = baseDao.selectPage(
                getpage,
                getWrapper(params)
        );
        return getPageData(page, SetmealsDTO.class);
    }
}